/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <fcntl.h>

#include <aos/kernel.h>
#include "aos/cli.h"
#include <k_api.h>
#include "sensor/sensor.h"

#include "linkkit/dev_model_api.h"
#include "linkkit/infra/infra_compat.h"
#include "linkkit/infra/infra_config.h"
#include "linkkit/infra/infra_defs.h"
#include "linkkit/infra/infra_types.h"
#include "linkkit/wrappers/wrappers.h"

#include "cJSON.h"
#include "linkkit_client.h"

#define PRODUCT_KEY "a1o4RZeCsWn"
#define PRODUCT_SECRET "FH3uiRCrqZQO8Lmf"
#define DEVICE_NAME "tycon_sensor_1"
#define DEVICE_SECRET "5xMUrYOkeBC26wTbm1z8FAudPGMB73hv"

/* modify to your wifi information */
#define WIFI_SSID "TYCON"
#define WIFI_PASSWD "TyCon1588"

/* sensor fd */
// static int acc_fd = -1;
static int temp_fd = -1;
static int humi_fd = -1;
static int baro_fd = -1;

static aos_timer_t post_timer;
static char post_payload[256] = {0};

static int get_temperature_data(float *dataT);
static int get_Humidity_data(float *dataH);
static int get_barometer_data(float *dataP);

/* sensor init */
static void sensor_init(void)
{
    printf("sensor_init...\r\n");
    /* open acc sensor */
    temp_fd = aos_open(DEV_TEMP_PATH(0), O_RDWR);
    humi_fd = aos_open(DEV_HUMI_PATH(0), O_RDWR);
    baro_fd = aos_open(DEV_BARO_PATH(0), O_RDWR);
    //其他传感器

    if (temp_fd < 0)
    {
        printf("temp sensor open failed !\n");
    }
    if (humi_fd < 0)
    {
        printf("humi sensor open failed !\n");
    }
    if (baro_fd < 0)
    {
        printf("baro sensor open failed !\n");
    }
}
static void sensor_post_task(void)
{
    float temp = 0;
    float humi = 0;
    float baro = 0;
    int ret = -1;

    while (1)
    {
        /* get temp sensor data */
        ret = get_temperature_data(&temp);
        if (ret != 0)
        {
            printf("temp sensor read failed !\n");
            return;
        }

        /* get humi sensor data */
        ret = get_Humidity_data(&humi);
        if (ret != 0)
        {
            printf("temp sensor read failed !\n");
            return;
        }

        /* get baro sensor data */
        ret = get_barometer_data(&baro);
        if (ret != 0)
        {
            printf("baro sensor read failed !\n");
            return;
        }
        /* post data to IoT platform */
        memset(post_payload, 0, 256);
        sprintf(post_payload, "{\"temp\":%.2f,\"humi\":%.2f,\"baro\":%.4f}", temp, humi, baro);
        printf("[sensor_post_task]:%s\r\n", post_payload);
        linkkit_set_props(post_payload, strlen(post_payload));
        /* delay for 1000ms */
        aos_msleep(1000);
    }
}

static int get_temperature_data(float *dataT)
{
    temperature_data_t data = {0};
    ssize_t size = 0;

    size = aos_read(temp_fd, &data, sizeof(data));
    if (size != sizeof(data))
    {
        return -1;
    }

    *dataT = (float)data.t / 10;
    return 0;
}

static int get_Humidity_data(float *dataH)
{
    humidity_data_t data = {0};
    ssize_t size = 0;

    size = aos_read(humi_fd, &data, sizeof(data));
    if (size != sizeof(data))
    {
        return -1;
    }

    *dataH = (float)data.h / 10;
    return 0;
}

static int get_barometer_data(float *dataP)
{
    barometer_data_t data = {0};
    ssize_t size = 0;

    size = aos_read(baro_fd, &data, sizeof(data));
    if (size != sizeof(data))
    {
        return -1;
    }

    *dataP = (float)data.p / 100000;

    return 0;
}
/* IoT platform connected callback */
int linkkit_connect_ok(void)
{
    aos_task_new("sensor_post", sensor_post_task, NULL, 4096);
    return 0;
}

/* application init, including sensor, wifi. */
void app_init(void)
{
    sensor_init();
    wifi_start(WIFI_SSID, WIFI_PASSWD);
}

/* start connecting to IoT platform */
void app_start(void)
{
    linkkit_connect(PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, PRODUCT_SECRET, NULL, linkkit_connect_ok);

    linkkit_start();
}
